﻿using System;
using MapInfo.Wrapper.MapOperations;
using JulMar.Windows.Mvvm;
using System.Windows.Input;

namespace NathansMapInfoProjects.WindowManager.ViewModels
{
    /// <summary>
    /// View model that represents a MapInfo window in MapInfo.
    /// </summary>
    public class WindowViewModel : TreeViewItemViewModel
    {
        private bool _isEditingName;
        private Window window;
        public event Action<WindowViewModel> WindowClosed;
        public event Action<WindowViewModel> WindowCloned;

        public WindowViewModel(GroupViewModel parent, Window window)
            : base(parent, false)
        {
            this.window = window;
            this.Group = parent;

            // Set up the binding for the commands.
            // This is where we join our view model commands to the associated mapbasic command.
            this.SwitchToEditingMode = new DelegatingCommand(() => IsEditingName = !IsEditingName);
            this.LockWindow = new DelegatingCommand(() => this.IsLocked = !this.IsLocked);
            this.BringToFront = new DelegatingCommand(() => this.window.BringToFront());
            this.Clone = new DelegatingCommand(() =>
                {
                    this.window.CloneWindow();
                    if (this.WindowCloned != null)
                    {
                        this.WindowCloned(this);
                    }
                });
            this.Close = new DelegatingCommand(() =>
                {
                    this.window.Close();
                    if (WindowClosed != null)
                    {
                        WindowClosed(this);
                    }
                });
        }

        public GroupViewModel Group { get; private set; }

        /// <summary>
        /// The ID of the window.
        /// </summary>
        public override int ID
        {
            get
            {
                return this.window.ID;
            }
        }

        /// <summary>
        /// The name of the window.
        /// </summary>
        public override string Name
        {
            get
            {
                return this.window.Name;
            }
            set
            {
                this.window.Name = value;
                base.OnPropertyChanged("Name");
                this._isEditingName = false;
            }
        }

        public bool IsLocked
        {
            get
            {
                if (Entry.Session.Eval(String.Format("WindowInfo({0},16)", this.window.ID)) == "F")
                    return true;
                else
                    return false;
            }
            set
            {
                string onoff = value ? "Off" : "On";

                Entry.Session.Do("Set Window " + this.window.ID + " SysMenuClose " + onoff);

                base.OnPropertyChanged("IsLocked");
            }
        }

        /// <summary>
        /// Gets or sets the view model into editing mode.
        /// </summary>
        public bool IsEditingName
        {
            get { return _isEditingName; }
            set { _isEditingName = value; OnPropertyChanged("IsEditingName"); }
        }

        /// <summary>
        /// Command used to switch to editing mode
        /// </summary>
        public ICommand LockWindow { get; private set; }

        /// <summary>
        /// Command used to switch to editing mode
        /// </summary>
        public ICommand SwitchToEditingMode { get; private set; }

        /// <summary>
        /// Command used by view to bring the window to the front of MapInfo.
        /// </summary>
        public ICommand BringToFront { get; private set; }

        /// <summary>
        /// Command used by view to clone the current window.
        /// </summary>
        public ICommand Clone { get; private set; }

        /// <summary>
        /// Command used by view to close the current window.
        /// </summary>
        public ICommand Close { get; private set; }
    }
}
